// function tweSum(arr, target) {
//   let len = arr.length;
//   for (let i = 0; i < len - 1; i++) {
//     for (let j = i + 1; j < len; j++) {
//       if (arr[i] + arr[j] === target) {
//         return [i, j];
//       }
//     }
//   }
//   return [];
// }

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function (nums, target) {
  let j, len = nums.length;
  let hash = {};
  for (let i = 0; i < len; i++) {
    j = target - nums[i];
    if (hash[j] !== undefined) {
      return [i, hash[j]];
    } else {
      hash[nums[i]] = i;
    }
  }
};

let arr = [2, 7, 11, 15];
let target = 9;
console.log(tweSum(arr, target));